package TomcatStudy;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;

/*
服务器类
 */
public class TomcatTest {
    private static int post = 5228;
    private static UrlUtil urlutil = new UrlUtil();

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
        System.out.println(" My Tomcat is Running");
        try {
            ServerSocket server = new ServerSocket(post);
            while (true) {
                Socket socket = server.accept();// 服务器每接受一次请求，就创建一个socket对象
                InputStream in = socket.getInputStream();
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(in));
                String info = null;
                String infoline = br.readLine();
                while (infoline != null) {
                    info =info+infoline;
                    infoline = br.readLine();
                }
                //UrlBean url = urlutil.readString(info);
                Class getclass = Class.forName("TomcatStudy.index.class");
                Method method = getclass.getMethod("show",null);
                method.invoke(getclass.newInstance(),null);

//                if (url != null) {
//                    String path=url.getPath();
//                    String className = url.getFileName();
//                    String methodName = url.getParameter().trim();
//                    ClassLoader classloader=ClassLoader.getSystemClassLoader();
//                    try {
//                        classloader.loadClass(path+"."+className);
//                        Class<?> getclass=Class.forName(path+"."+className);
//                        Method method=getclass.getMethod(methodName, null);
//                        method.invoke(getclass.newInstance(), null);
//
//                    } catch (ClassNotFoundException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    } catch (NoSuchMethodException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    } catch (SecurityException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    } catch (IllegalAccessException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    } catch (IllegalArgumentException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    } catch (InvocationTargetException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    } catch (InstantiationException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    }
//                } else {
//
//                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


}
